from flask import url_for
from app.models import User


def user_schema(user):
    return {
        'id': user.id,
        'username': user.username,
        'email': user.email,
    }


def crawler_schema(crawler):
    return {
        'id': crawler.id,
        'name': crawler.name,
        'category': crawler.category.title
    }


def crawlers_schema(items, pagination):
    return {
        'count': pagination.total,
        'totalPages': pagination.pages,
        'current': pagination.page,
        'data': [crawler_schema(item) for item in items]
    }


def category_schema(category):
    return {
        'id': category.id,
        'title': category.title,
    }


def record_schema(record):
    return {
        'id': record.id,
        'title': record.title,
        'url': record.url,
        'heat': record.heat,
        'created': record.timestamp
    }


def records_schema(items, pagination):
    return {
        'count': pagination.total,
        'totalPages': pagination.pages,
        'current': pagination.page,
        'data': [record_schema(item) for item in items]
    }
